package servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import pojo.Book;

import java.io.IOException;
import java.util.List;

@WebServlet("/searchBooks")
public class BookServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String searchTitle = request.getParameter("searchTitle");
        BookDAO bookDAO = new BookDAO();
        List<Book> bookList = null;
        String message = null;

        try {
            bookList = bookDAO.searchBooksByTitle(searchTitle);
            request.setAttribute("bookList", bookList);
            request.setAttribute("searchTitle", searchTitle);

            if (bookList.isEmpty()) {
                message = "未找到书籍！";
            }
            request.setAttribute("message", message);


            request.getRequestDispatcher("bookDetails.jsp").forward(request, response);
        } finally {
            bookDAO.close();
        }
    }
}